package dao

import (
	"go-programming-tour-book/blog_service/internal/model"
	"go-programming-tour-book/blog_service/pkg/app"
)

func (d *Dao) CountArticle(title, desc, content, coverImageUrl string, state uint8) (int, error) {
	article := model.Article{
		Title:         title,
		Desc:          desc,
		Content:       content,
		CoverImageUrl: coverImageUrl,
		State:         state,
	}
	return article.Count(d.engine)
}

func (d *Dao) GetArticleList(title string, state uint8, page, pageSize int) ([]*model.Article, error) {
	article := model.Article{Title: title, State: state}
	pageOffset := app.GetPageOffset(page, pageSize)
	return article.List(d.engine, pageOffset, pageSize)
}

func (d *Dao) CreateArticle(title, desc, content, coverImageUrl string, state uint8, createdBy string) error {
	article := model.Article{
		Model:         &model.Model{CreatedBy: createdBy},
		Title:         title,
		Desc:          desc,
		Content:       content,
		CoverImageUrl: coverImageUrl,
		State:         state,
	}
	return article.Create(d.engine)
}

func (d *Dao) UpdateArticle(id uint32, title, desc, content, coverImageUrl string, state uint8, modifiedBy string) error {
	article := model.Article{
		Model: &model.Model{ID: id},
	}
	values := map[string]interface{}{
		"desc":          desc,
		"content":       content,
		"coverImageUrl": coverImageUrl,
		"state":         state,
		"modified_by":   modifiedBy,
	}
	if title != "" {
		values["title"] = title
	}
	return article.Update(d.engine, values)
}

func (d *Dao) DeleteArticle(id uint32) error {
	article := model.Article{Model: &model.Model{ID: id}}
	return article.Delete(d.engine)
}
